//*************************************************************************************************
//************************ (C) 2001 - 2006, Eleven Engineering Incorporated ***********************
//*************************************************************************************************
//**
//**		Tabs:	This file looks best with tab stops set every 6 spaces.
//**
//*************************************************************************************************
//*************************************************************************************************
//**
//**          File:  RandomNumber.asm
//**       Project:  XInc2 Library
//**       Created:  04 Jan 2003 by Ryan Northcott
//**       Revised:  08 Sep 2006 by Dan Beutel
//**
//**   Description:  A routine for generating random numbers using differences
//**                 between the RC clock and the crystal oscillator.
//**
//** Compatibility:  Requires XInA v4.4 or higher.
//**
//**    Disclaimer:  You may incorporate this sample source code into your program(s) without
//**                 restriction.  This sample source code has been provided "AS IS" and the
//**                 responsibility for its operation is yours.  You are not permitted to
//**                 redistribute this sample source code as "Eleven sample source code" after
//**                 having made changes.  If you are going to re-distribute the source, we require
//**                 that you make it clear in the source that the code was descended from Eleven
//**                 sample source code, but that you have made changes.
//**
//*************************************************************************************************
//*************************************************************************************************
//**
//**	GetRandomNumber
//**
//*************************************************************************************************
//*************************************************************************************************

#ifndef __RANDOM_NUMBER__
#define __RANDOM_NUMBER__

GetRandomNumber:
					st	r1, sp, 0
					st	r6, sp, 1
					add	sp, sp, 2
					
	GetRandomNumber_start:
					mov	r0, 0x1911

					inp	r1, SCUtime
		GetRandomNumber_loop0:
					inp	r6, SCXclkCfg
					bic	r6, r6, 14
					bc	VS, GetRandomNumber_loop0
					inp	r1, SCUtime
					add	r1, r1, r6
			GetRandomNumber_loop0_wait:
					inp	r6, SCUtime
					sub	r6, r6, r1
					bc	LT0, GetRandomNumber_loop0_wait

					rol	r0, r0, 7
					add	r0, r0, r1

					inp	r1, SCUtime
		GetRandomNumber_loop1:
					inp	r6, SCXclkCfg
					bic	r6, r6, 14
					bc	VC, GetRandomNumber_loop1
					inp	r6, SCUtime
					add	r1, r1, r6
			GetRandomNumber_loop1_wait:
					inp	r6, SCUtime
					sub	r6, r6, r1
					bc	LT0, GetRandomNumber_loop1_wait

					rol	r0, r0, 7
					add	r0, r0, r1

					inp	r1, SCUtime
		GetRandomNumber_loop2:
					inp	r6, SCXclkCfg
					bic	r6, r6, 14
					bc	VS, GetRandomNumber_loop2
					inp	r6, SCUtime
					add	r1, r1, r6
			GetRandomNumber_loop2_wait:
					inp	r6, SCUtime
					sub	r6, r6, r1
					bc	LT0, GetRandomNumber_loop2_wait

					rol	r0, r0, 7
					add	r0, r0, r1
					bc	ZS, GetRandomNumber_start

GetRandomNumber_END:
					sub	sp, sp, 2
					ld	r6, sp, 1
					ld	r1, sp, 0

					jsr	r6, r6

#endif